﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using log4net;
using CommandLine;

namespace GenerateDB
{
    class Program
    {
        static void Main(string[] args)
        {
#if (!DEBUG)
            try
            {
#endif
            var options = new Options();
            ICommandLineParser parser = new CommandLineParser();

            if (parser.ParseArguments(args, options))
            {
                DataBaseInfo dbInfo = new DataBaseInfo { Server = options.DatabaseServer, Username = options.DatabaseUser, Password = options.DatabasePassword };

                DatabaseGenerator dbGenerator = new DatabaseGenerator(dbInfo);

                if (options.GenerateDatabase)
                    dbGenerator.Generate();

                string xsdScheme = options.XSDSchemaJournal;
                foreach (var journal in options.Journals.Split())
                {
                    XMLCheckerByXSD ch = new XMLCheckerByXSD(journal, xsdScheme);
                    ch.Check();

                    ParserXMLJournal parserjournal = new ParserXMLJournal(journal);

                    dbGenerator.GenerateJournal(parserjournal.ParseJournal());
                }
            }
            else
            {
                throw new Exception("Аргументы командной строки указаны неверно.");
            }
#if (!DEBUG)
            }
            catch (Exception ex)
            {
                Console.WriteLine("Ошибка: {0}", ex.Message);
            }
#endif
            Console.WriteLine("Нажмите любую клавишу...");
            Console.Read();
        }
    }
}
